package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.service.OrderService;
import com.itheima.service.OrdersettinglistService;
import com.itheima.utils.DateUtils;
import com.itheima.utils.SMSUtils;
import org.aspectj.weaver.ast.Or;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("ordersettinglist")
public class OrdersettinglistController {

    @Reference
    private OrdersettinglistService ordersettinglistService;

    @Reference
    private OrderService orderService;

    /**
     * 分页查询,条件查询预约信息
     * @param qpb
     * @return
     */
    @RequestMapping("findPage")
    public Result findPage(@RequestBody QueryPageBean qpb){
        try {
            PageResult pageResult = ordersettinglistService.findByPage(qpb);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    /**
     * 新增一条预约(电话预约)
     * @param setmealId
     * @param formData
     * @return
     */
    @RequestMapping("add")
    public Result add(String setmealId,@RequestBody Map<String,String> formData){
        if ("".equals(setmealId)||setmealId==null)
            return new Result(false,MessageConstant.ORDER_WITHOUT_SETMEAL);
        formData.put("orderType", Order.ORDERTYPE_TELEPHONE);
        formData.put("setmealId", setmealId);
        Result result;
        try {
            result = orderService.order(formData);
        } catch (Exception e) {
            e.printStackTrace();
            result = new Result(false,MessageConstant.DUBBO_ERROR);
        }
        //预约成功后,给用户发送预约成功的短信
        if (result.isFlag()){
            try {
                SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE,formData.get("telephone"),formData.get("orderDate"));
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    /**
     * 确认预约,更改用户的预约状态为"已预约",并给用户发短信通知
     * @param order
     * @return
     */
    @RequestMapping("/confirm")
    public Result confirm(@RequestBody Order order){
        try {
            String phoneNumber = order.getMember().getPhoneNumber();
            orderService.confirm(order.getId());
            String orderDate = DateUtils.parseDate2String(order.getOrderDate());
            SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE,phoneNumber,orderDate);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDER_NOTICE_FAIL);
        }
        return new Result(true,MessageConstant.ORDER_NOTICE_SUCCESS);
    }

    /**
     * 删除一条预约记录
     * @param order
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(@RequestBody Order order){
        try {
            ordersettinglistService.delete(order);
            SMSUtils.sendShortMessage(SMSUtils.CANCEL_ORDER_NOTICE,order.getMember().getPhoneNumber(),DateUtils.parseDate2String(order.getOrderDate()));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_ORDER_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_ORDER_SUCCESS);
    }

    /**
     * 批量进行删除预约信息
     * @param orders
     * @return
     */
    @RequestMapping("/deleteSelection")
    public Result deleteSelection(@RequestBody List<Order> orders){
        if (orders.size()==0||orders==null)
            return new Result(false,MessageConstant.ORDERS_NULL);
        for (Order order : orders) {
            try {
                ordersettinglistService.delete(order);
                SMSUtils.sendShortMessage(SMSUtils.CANCEL_ORDER_NOTICE,order.getMember().getPhoneNumber(),DateUtils.parseDate2String(order.getOrderDate()));
            } catch (Exception e) {
                e.printStackTrace();
                return new Result(false,MessageConstant.DELETE_ORDER_FAIL);
            }
        }
        return new Result(true,MessageConstant.DELETE_ORDER_SUCCESS);
    }

}
